package com.definesys.smecrailtransit.module.RealStatus;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.bean.LineBean;
import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.module.RealStatus.bean.ELeRealNumBean;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

import static com.definesys.smecrailtransit.module.RealStatus.RealStatusFragment.GET_REAL_ELE_NUM_FAIL;
import static com.definesys.smecrailtransit.module.RealStatus.RealStatusFragment.GET_REAL_ELE_NUM_SUCCESS;

/**
 * Created by cjy on 2017/8/23.
 */

public class RealStatusPresenter extends SmecBasePresenter implements RealStatusConstract {

    public RealStatusPresenter(Context mContext) {
        super(mContext);
    }


    @Override
    public void getRealEleNum(HashMap<String, String> map) {
        Observable<HttpResult<ELeRealNumBean>> observable=smecRailService.getRealEleNum(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ELeRealNumBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_REAL_ELE_NUM_FAIL,new Object());
            }

            @Override
            public void onNext(HttpResult<ELeRealNumBean> eLeRealNumBeanHttpResult) {
                ELeRealNumBean arrayList= eLeRealNumBeanHttpResult.getData();
                ArrayList<LineBean> lineList = arrayList.getLine();
                ArrayList<ApplyBean> applyList = arrayList.getApply();
                ArrayList<StationBean> stationList = arrayList.getStation();

                for (LineBean lineBean : lineList) {
                    lineBean.setFlag(SmecRailApi.MonthCode.REALSTATUS);
                }
                for (ApplyBean applyBean : applyList) {
                    applyBean.setFlag(SmecRailApi.MonthCode.REALSTATUS);
                }
                for (StationBean stationBean : stationList) {
                    stationBean.setFlag(SmecRailApi.MonthCode.REALSTATUS);
                }

                realmManager.insertOrUpdateBatch(lineList);
                realmManager.insertOrUpdateBatch(applyList);
                mRxbus.post(GET_REAL_ELE_NUM_SUCCESS,eLeRealNumBeanHttpResult.getData());
            }
        });
    }
}
